<!DOCTYPE html>
<html>

<head>
    {{template "header"}}
</head>

<body>
<script>
    let isSuper = "{{.oprlevel}}"==="super";
    let tableid = webix.uid()
    let getColumns = function () {
        return [
            {
                cols: [
                    {view: "text", name: "name", label: tr("cwmp","Name"), css: "nborder-input",},
                    {view: "text", name: "oid", label: tr("cwmp","OID"), css: "nborder-input",},
                ]
            },
            {
                cols: [
                    {view: "combo", name: "level", label: tr("cwmp","Level"), options: ["normal", "major", "security"], css: "nborder-input",},
                    {view: "counter", name: "timeout", label: tr("cwmp","Timeout secs"), min: 60, max: 600, css: "nborder-input",},
                ]
            },
            {
                cols: [
                    {view: "text", name: "target_filename", label: tr("cwmp","Target filename"), css: "nborder-input",},
                    {view: "text", name: "software_version", label: tr("cwmp","Version"), css: "nborder-input",},
                ]
            },
            {view: "text", name: "task_tags", label: tr("cwmp","Task tags"), css: "nborder-input",},
            {name: "content", view: "codemirror-editor", mode: "shell"},
        ]
    }

    let deleteItem = function (ids, callback) {
        webix.confirm({
            title: gtr("Operation confirmation"),
            ok: gtr("Yes"), cancel: gtr("No"),
            text: gtr("Confirm to delete? This operation is irreversible."),
            callback: function (ev) {
                if (ev) {
                    webix.ajax().get('/admin/cwmp/config/delete', {ids: ids}).then(function (result) {
                        let resp = result.json();
                        webix.message({type: resp.msgtype, text: resp.msg, expire: 2000});
                        if (callback)
                            callback()
                    }).fail(function (xhr) {
                        webix.message({type: 'error', text: gtr("Delete Failure:") + xhr.statusText, expire: 2000});
                    });
                }
            }
        });
    }

    webix.ready(function () {
        let queryid = webix.uid().toString()
        let importUrl = '/admin/cwmp/config/import'
        let exportUrl = '/admin/cwmp/config/export'
        let tableid = webix.uid().toString();
        let uploadid = webix.uid().toString();
        let reloadData = wxui.reloadDataFunc(tableid, "/admin/cwmp/config/query", queryid)
        wxui.initUploadApi(uploadid, importUrl, reloadData);
        webix.ui({
            css: "main-panel",
            padding: 7,
            rows: [
                wxui.getPageToolbar({
                    title: tr("cwmp", "Tr069 config"),
                    icon: "mdi mdi-code-braces",
                    elements: [
                        wxui.getPrimaryButton(gtr("Edit"), 90, false, function () {
                            let item = $$(tableid).getSelectedItem();
                            if (item) {
                                let vitem = webix.copy(item)
                                wxui.openFormWindow({
                                    fullscreen: true,
                                    width: 640,
                                    height: 640,
                                    title: tr("cwmp", "Edit tr069 config"),
                                    data: vitem,
                                    post: "/admin/cwmp/config/update",
                                    callback: reloadData,
                                    elements: getColumns()
                                }).show();
                            } else {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            }
                        }),
                        wxui.getPrimaryButton(gtr("Clone"), 90, !isSuper, function () {
                            let item = $$(tableid).getSelectedItem();
                            if (item) {
                                let vitem = webix.copy(item)
                                vitem.id = ""
                                wxui.openFormWindow({
                                    fullscreen: true,
                                    width: 640,
                                    height: 640,
                                    title: tr("cwmp", "Clone tr069 config"),
                                    data: vitem,
                                    post: "/admin/cwmp/config/add",
                                    callback: reloadData,
                                    elements: getColumns()
                                }).show();
                            } else {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            }
                        }),
                        wxui.getPrimaryButton(gtr("Create"), 90, !isSuper, function () {
                            wxui.openFormWindow({
                                fullscreen: true,
                                width: 640,
                                height: 640,
                                title: tr("cwmp", "Create tr069 config"),
                                post: "/admin/cwmp/config/add",
                                callback: reloadData,
                                elements: getColumns()
                            }).show();
                        }),
                        wxui.getDangerButton(gtr("Remove"), 90, !isSuper, function () {
                            let rows = wxui.getTableCheckedIds(tableid);
                            if (rows.length === 0) {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            } else {
                                deleteItem(rows.join(","), reloadData);
                            }
                        }),
                    ]
                }),
                wxui.getTableQueryCustomForm(queryid, [
                    {
                        cols: [
                            {view: "search", id: "keyword", name: "keyword", placeholder: "keywords", width: 320},
                            {
                                view: "button",
                                label: gtr("Query"),
                                css: "webix_transparent",
                                type: "icon",
                                icon: "mdi mdi-search-web",
                                borderless: true,
                                width: 100,
                                click: function () {
                                    reloadData()
                                }
                            }, {}
                        ]
                    }
                ]),
                wxui.getDatatable({
                    tableid: tableid,
                    url: '/admin/cwmp/config/query',
                    columns: [
                        {
                            id: "state",
                            header: {content: "masterCheckbox", css: "center"},
                            headermenu: false,
                            adjust: true, width: 45,
                            css: "center",
                            template: "{common.checkbox()}"
                        },
                        {
                            id: "name",
                            header: [tr("cwmp", "Name")],
                            fillspace: true,
                            sort: "server",
                        },
                        {
                            id: "oid",
                            header: [tr("cwmp", "OID")],
                            fillspace: true,
                            sort: "server",
                        },
                        {
                            id: "target_filename",
                            header: [tr("cwmp", "Target filename")],
                            fillspace: true,
                            sort: "server",
                        },
                        {
                            id: "level",
                            header: [tr("cwmp","Level")],
                            adjust: true,
                            sort: "server",
                        },
                        {
                            id: "timeout",
                            header: [tr("cwmp","Timeout")],
                            adjust: true,
                            sort: "server",
                        },
                        {
                            id: "updated_at",
                            header: [tr("cwmp","Updated")],
                            adjust: true,
                            sort: "server",
                        },
                        // {header: {content: "headerMenu"}, headermenu: false, width: 35}
                    ],
                    leftSplit: 1,
                    pager: true,
                }),
                wxui.getTableFooterBar({
                    tableid: tableid,
                    callback: reloadData,
                    actions: [
                    ],
                }),
            ]
        })
    })
</script>
</body>

</html>

